home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 March: Reference Library / Dev.CD Mar 97 RL.toast / mac / Technical Documentation / develop / develop Issue 27 / develop Issue 27 code / Internet Config Assistant / toolkit / CDialogs.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-30  |  7.9 KB  |  345 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CDialogs.h
  3.  
  4.     Contains:    Layer built on top of the Dialog Manager
  5.  
  6.     Written by:    Arno Gourdol
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #pragma once
  13.  
  14. #ifndef __CDIALOGS__
  15. #define __CDIALOGS__
  16.  
  17. #include <Dialogs.h>
  18. #include <ToolUtils.h>
  19.  
  20. #include "CWindows.h"
  21. #include "CFonts.h"
  22.  
  23. #define kDialogParametersCount 16    // max number of parameter text in a dialog
  24. #define kStyledTextCount 32    // max number of items with styled text
  25.  
  26. struct DialogParameter
  27. {
  28.     Str15 key;            // The string that will be replaced,
  29.                         // for example "<name>"
  30.     Str255 value;        // The value by which the key will 
  31.                         // be replaced
  32.     SInt16 dialogItem;    // If the dialog item only contains the 
  33.                         // key, index of the dialog item
  34. };
  35.  
  36.  
  37. class CDialog : public CWindow
  38. {
  39. public:
  40.     // constructor
  41.     CDialog(SInt16 dialogID, FontCode fontCode = kSystemFont);
  42.         
  43.     // destructor
  44.     virtual ~CDialog(void);        
  45.  
  46.     // modifiers
  47.     virtual WindowRef MakeWindow(void);
  48.     virtual DialogRef MakeDialog(void);
  49.     virtual void Close(void);
  50.  
  51.     virtual void Draw(TDrawContext& drawContext);
  52.  
  53.     // modifiers
  54.     virtual void ItemHit(short itemHit);
  55.     void DoPrepareDialog(void);
  56.     virtual void PrepareDialog(void);
  57.     virtual void SubstituteParameters(void);
  58.     virtual void DrawUserItem(TDrawContext& drawContext, short item, const CRect& frame);
  59.     void SetParameter(ConstStr15Param key, ConstStr255Param value);
  60.     void GetParameter(ConstStr15Param key, Str255 value);
  61.     inline void SetDefaultButton(short itemNumber);
  62.     inline void SetCancelButton(short itemNumber);
  63.     inline void SetDefaultButtonName(ConstStr63Param buttonName);        
  64.     virtual Boolean FilterEvent(const EventRecord &event);
  65.     inline void SetNextDialog(CDialog *theDialog);
  66.     void AddKeyboardShortcuts(short stringID);
  67.         // all strings in the STR# take the form "X:N", where X is the key and N the item number
  68.     
  69.     // Keyboard navigation
  70.     short FirstTextField(void);
  71.     short LastTextField(void);
  72.     short PreviousTextField(short item = 0);
  73.     short NextTextField(short item = 0);
  74.  
  75.     // selectors
  76.     inline DialogRef GetDialogRef(void);        
  77.     GDHandle GetMaxIntersectedDevice(class CRect& screenRect);
  78.     static CDialog* GetCDialog(DialogRef dialog);
  79.     inline CDialog* GetNextDialog(void);
  80.     virtual Boolean CheckKeyboardShortcuts(const EventRecord &event);
  81.     TEHandle GetStyledText(short item);
  82.     void SetStyledText(short item, TEHandle styledText);
  83.  
  84.     // dialog utilities
  85.     inline void SetDrawingProc(short item, UserItemUPP proc);
  86.     inline CRect GetItemRect(short item);
  87.     void SetItemRect(short item, const CRect &newRect);
  88.     inline ControlRef GetItemControl(short item);
  89.     void SetItemControl(short item, ControlRef control);
  90.     inline Handle GetItemData(short item);
  91.     inline void SetItemData(short item, Handle data);
  92.     inline void GetItemText(short item, Str255 s);
  93.     void SetItemText(short item, ConstStr255Param s);
  94.     inline void SelectItemText(short item, short start = 0, short finish = 32767);
  95.     inline void SetItemValue(short item, short value);
  96.     inline short GetItemValue(short item);
  97.     inline short GetItemHilite(short item);
  98.     inline void HiliteItem(short item, short hilite);
  99.     inline void EnableItem(short item, Boolean enable = true);
  100.     inline void DisableItem(short item);
  101.     inline Boolean IsEnabled(short item);
  102.     inline void GetItemTitle(short item, Str255 title);
  103.     inline void SetItemTitle(short item, ConstStr255Param title);
  104.     inline short GetItemType(short item);
  105.     inline void SetItemType(short item, short theType);
  106.     inline void HideItem(short item);
  107.     inline void ShowItem(short item);
  108.     inline void FlashButton(short item);
  109.     inline void GetSelectionEndpoints(short &start, short &end);
  110.     inline short GetItemTextSize(short item = 0);
  111.     void PasteText(Ptr text, short size);
  112.     
  113.     CFontSpec fDialogFont;
  114.  
  115. protected:
  116.     SInt16 fDialogID;
  117.     DialogRef fDialog;
  118.  
  119.     short fDefaultButton;            // item number of the default button    
  120.     short fCancelButton;            // item number of the cancel button
  121.  
  122.     UserItemUPP fUserItemCallback;
  123.     
  124.     // Data for parameter substitution
  125.     DialogParameter fParameters[kDialogParametersCount];
  126.  
  127.     // Private data stored with each item
  128.     short fStyleTextItem[kStyledTextCount];
  129.     TEHandle fStyledText[kStyledTextCount];
  130.  
  131.     // linked list of dialogs used to turn dialog pointers into CDialogs
  132.     static CDialog *gDialogList;
  133.     CDialog* fNextDialog;
  134.  
  135.     // keyboard shortcuts array
  136.     short fNumShortcuts;
  137.     struct Shortcut { UInt16 key; UInt16 item; } *fShortcuts;
  138.  
  139. private:
  140.     static pascal void UserItemCallback(DialogRef dialog, short item);
  141. };
  142.  
  143. void CDialog::SetNextDialog(CDialog *theDialog)
  144. {
  145.     fNextDialog = theDialog;
  146. }
  147.  
  148. DialogRef CDialog::GetDialogRef(void)
  149. {
  150.     return fDialog;
  151. }
  152.  
  153. CDialog* CDialog::GetNextDialog(void)
  154. {
  155.     return fNextDialog;
  156. }
  157.  
  158. void CDialog::SetDrawingProc(short item, UserItemUPP proc)
  159. {
  160.     assert(GetItemType(item) == userItem);
  161.     SetItemData(item, (Handle)proc);
  162. }
  163.  
  164. CRect CDialog::GetItemRect(short item)
  165. {
  166.     Handle h;
  167.     short type;
  168.     CRect r;
  169.     
  170.     GetDialogItem(GetDialogRef(), item, &type, &h, r);
  171.     
  172.     return r;
  173. }
  174.  
  175. Handle CDialog::GetItemData(short item)
  176. {
  177.     Rect r;
  178.     Handle h;
  179.     short type;
  180.     
  181.     GetDialogItem(GetDialogRef(), item, &type, &h, &r);
  182.     return h;
  183. }
  184.  
  185. void CDialog::SetItemData(short item, Handle data)
  186. {
  187.     Rect r;
  188.     Handle h;
  189.     short type;
  190.     
  191.     GetDialogItem(GetDialogRef(), item, &type, &h, &r);
  192.     SetDialogItem(GetDialogRef(), item, type, data, &r);
  193. }
  194.  
  195. void CDialog::GetItemText(short item, Str255 s)
  196. {
  197.     assert(GetItemType(item) == statText || GetItemType(item) == editText);
  198.     
  199.     GetDialogItemText(GetItemData(item), s);
  200. }
  201.  
  202. void CDialog::SelectItemText(short item, short start, short finish)
  203. {
  204.     assert(GetItemType(item) == editText);
  205.  
  206.     SelectDialogItemText(GetDialogRef(), item, start, finish);
  207. }
  208.  
  209. ControlRef CDialog::GetItemControl(short item)
  210. {
  211.     Rect r;
  212.     Handle h;
  213.     short type;
  214.     
  215.     GetDialogItem(GetDialogRef(), item, &type, &h, &r);
  216.     assert((type & ctrlItem) == ctrlItem);
  217.     return (ControlRef)h;
  218. }
  219.  
  220. void CDialog::SetItemValue(short item, short value)
  221. {
  222.     ControlRef control = GetItemControl(item);
  223.     if (value != GetControlValue(control))
  224.         SetControlValue(control, value);
  225. }
  226.  
  227. short CDialog::GetItemValue(short item)
  228. {
  229.     return GetControlValue(GetItemControl(item));
  230. }
  231.  
  232. short CDialog::GetItemHilite(short item)
  233. {    
  234.     short result = (**GetItemControl(item)).contrlHilite;
  235.     return result;
  236. }
  237.  
  238.  
  239. void CDialog::HiliteItem(short item, short hilite)
  240. {
  241.     if (GetItemHilite(item) != hilite)
  242.     {
  243.         HiliteControl(GetItemControl(item), hilite);
  244.     }
  245. }
  246.  
  247. void CDialog::EnableItem(short item, Boolean enable)
  248. {
  249.     HiliteItem(item, enable ? 0 : 255);
  250.     assert(IsEnabled(item) == enable);
  251. }
  252.  
  253. void CDialog::DisableItem(short item)
  254. {
  255.     HiliteItem(item, 255);
  256.     assert(!IsEnabled(item));
  257. }
  258.  
  259. Boolean CDialog::IsEnabled(short item)
  260. {
  261.     return GetItemHilite(item) != 255;
  262. }
  263.  
  264. void CDialog::GetItemTitle(short item, Str255 title)
  265. {
  266.     GetControlTitle(GetItemControl(item), title);
  267. }
  268.  
  269. void CDialog::SetItemTitle(short item, ConstStr255Param title)
  270. {
  271.     ControlRef control = GetItemControl(item);
  272.     Str255 s;
  273.     GetControlTitle(control, s);
  274.     if (!EqualString(s, title, true, true))
  275.         SetControlTitle(control, title);    
  276. }
  277.  
  278. short CDialog::GetItemType(short item)
  279. {
  280.     Rect r;
  281.     Handle h;
  282.     short type;
  283.     
  284.     GetDialogItem(GetDialogRef(), item, &type, &h, &r);
  285.  
  286.     return type & ~itemDisable;
  287. }
  288.  
  289. void CDialog::SetItemType(short item, short theType)
  290. {
  291.     Rect r;
  292.     Handle h;
  293.     short type;
  294.     
  295.     GetDialogItem(GetDialogRef(), item, &type, &h, &r);
  296.     SetDialogItem(GetDialogRef(), item, theType | (type & itemDisable), h, &r);
  297. }
  298.  
  299. void CDialog::HideItem(short item)
  300. {
  301.     HideDialogItem(GetDialogRef(), item);
  302. }
  303.  
  304. void CDialog::ShowItem(short item)
  305. {
  306.     ShowDialogItem(GetDialogRef(), item);
  307. }
  308.  
  309. void CDialog::FlashButton(short item)
  310. {
  311.     if (IsEnabled(item))
  312.     {
  313.         long waitTicks;
  314.         HiliteItem(item, kControlButtonPart);
  315.         Delay(8, &waitTicks);
  316.         HiliteItem(item, 0);
  317.     }
  318. }
  319.  
  320. void CDialog::GetSelectionEndpoints(short& start, short& end)
  321. {
  322.     TEHandle te = ((DialogPeek)GetDialogRef())->textH;
  323.     end = (**te).selEnd;
  324.     start = (**te).selStart;
  325. }
  326.  
  327. short CDialog::GetItemTextSize(short item)
  328. {
  329.     Str255 s;
  330.     if (item == 0)
  331.     {
  332.         GetItemText(((DialogPeek)GetDialogRef())->editField + 1, s);
  333.     }
  334.     else
  335.     {
  336.         GetItemText(item, s);
  337.     }
  338.     return s[0];
  339. }
  340.  
  341.  
  342.  
  343.  
  344. #endif
  345.